<?php
Core::getInstance()->app_load_class('Admin',false);
Core::getInstance()->sys_load_class('FileUpload',false);
Core::getInstance()->sys_load_class('Model',false);
Core::getInstance()->sys_load_class('Character',false);
class Article extends Admin{
	/*Singleton{{{*/
	public static function getInstance(){
		static $instance=null;
		if($instance===null){ 
			$instance = new self();
		}
		return $instance;
	}
	private function __construct(){}
	private function __clone(){
		trigger_error("Clone is not allowed.", E_USER_ERROR);
	}
	/*}}}*/

	public function addPicAction(){
		include_once(WEB_ROOT."syntaxhighlighter/fckeditor/fckeditor.php");
		$FCKeditor=new FCKeditor('desc');
		$FCKeditor->BasePath='./syntaxhighlighter/fckeditor/';
		$FCKeditor->ToolbarSet='Basic';
		$FCKeditor->Width='500px';
		$FCKeditor->Height='300px';

		$title= '添加图片文章';
		if(count($_FILES)>0 || count($_POST)>0 ){ 
			$art  = Core::getInstance()->app_load_class('ArticleModel');
			if(isset($_POST['id']) && !empty($_POST['id'])){ // 更新图片文章
				$art->addPicArticle($_POST['id']);
			}else{ // 添加图片文章
				$art->addPicArticle();
			}
			header('Location:?m=admin&c=article&a=listPic');
		}


		$categorys = Core::getInstance()->app_load_class('ArticleModel')->lists();
		include $this->display('article_add.tpl.php');
	}

	// 修改文章
	public function modifyPicAction(){
		if(isset($_GET['id']) &&!empty($_GET['id'])){
			include_once(WEB_ROOT."syntaxhighlighter/fckeditor/fckeditor.php");
			$id = intval($_GET['id']);
			$art  = Core::getInstance()->app_load_class('ArticleModel');
			$categorys = $art->lists();
			$art->_table = 'article';
			$data = $art->getOneData($id);

			$title='修改图片文章';
			$FCKeditor=new FCKeditor('desc');
			$FCKeditor->BasePath='./syntaxhighlighter/fckeditor/';
			$FCKeditor->ToolbarSet='Basic';
			$FCKeditor->Width='500px';
			$FCKeditor->Height='300px';
			$FCKeditor->Value = Character::HTMLToDbDecode($data['desc']);
			include $this->display('article_add.tpl.php');
		}else{
			header('Location:?m=admin&c=article&c=listPic');
		}
	}


	// 图片文章列表
	public function listPicAction(){
		$title= '图片文章列表';
		
		$m = Core::getInstance()->app_load_class('ArticleModel');
		$m->_table = 'article';
		if(isset($_GET['id'])){
			$m->delData($_GET['id']);
		}
		$data = $m->lists();
		include $this->display('article_pic_list.tpl.php');
	}

	// 删除附件
	public function removeAttachmentAction(){
		if(isset($_GET['id']) && isset($_GET['uploadfile'])){
			$art = Core::getInstance()->app_load_class('ArticleModel');
			$art->removeAttachment($_GET['id'],$_GET['uploadfile']);
		}
	}

	public function modifyPicPageAction(){
	}

	/*分类{{{*/
	public function listCategoryAction(){
		$title= '分类列表';
		$m = Core::getInstance()->app_load_class('ArticleModel');
		if(isset($_GET['id'])){
			$m->delData($_GET['id']);
		}
		$data = $m->lists();
		include $this->display('article_list_category.tpl.php');
	}

	/** * 添加分类 */
	public function addCategoryAction(){
		$title='添加分类';
		$m = Core::getInstance()->app_load_class('ArticleModel');
		if(count($_POST)>=1){
			if(isset($_POST['id'])){
				$m->updateCategory($_POST);
			}else{
				$m->addCategory($_POST);
			}
			header('Location:?m=admin&c=article&a=listCategory');
		}elseif(isset($_GET['id'])){
			$title = '修改分类';
			$id = intval($_GET['id']);
			$data = $m->getOneData($id);
		}
		include $this->display('article_add_category.tpl.php');

	}

	/** 修改模版 */
	public function modifyCategoryTplAction(){
		$title='修改分类模版';

		$m= Core::getInstance()->app_load_class('ArticleModel');
		if(count($_POST)>0){
			$m->modifyCategoryTpl($_POST['tpl'],$_POST['id']);
			header('Location:?m=admin&c=article&a=listCategory');
		}
		if(!isset($_GET['id']) && !empty($_GET['id'])) return false;
		$id = intval($_GET['id']);
		$data = $m->getOneData($id);
		if(empty($data)) return false;

		include $this->display('article_modify_category_tpl.tpl.php');
	}
	/*}}}*/
}
